package cn.yshujia.handler;

import cn.yshujia.constant.SystemConst;
import cn.yshujia.domain.vo.ApiVO;
import cn.yshujia.enums.Info;
import com.alibaba.fastjson2.JSON;
import jakarta.servlet.ServletOutputStream;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.logout.LogoutSuccessHandler;
import org.springframework.stereotype.Component;

import java.io.IOException;

/**
 * @author YShuJia
 * @create 2024/4/23
 * @description 退出成功处理器
 */

@Component
public class LogoutSuccessHandle implements LogoutSuccessHandler {
	
	public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException {
		response.setContentType("application/json;charset=UTF-8");
		ServletOutputStream outputStream = response.getOutputStream();
		String token = request.getHeader(SystemConst.TOKEN_KEY);
		ApiVO<Boolean> apiVO = ApiVO.success(Info.SUCCESS.getCode(), "退出成功！", true);
		outputStream.write(JSON.toJSONBytes(apiVO));
		outputStream.flush();
		outputStream.close();
	}
}